package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加菜品
     *
     * @param category
     * @return
     */
    @PostMapping
    public R add(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 查看菜品
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R select(Integer page, Integer pageSize) {
        IPage<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 删除菜品
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById(long id) {
        //通过id获取category对象
        Category category = categoryService.getById(id);
        //通过对象获取类型
        Integer type = category.getType();
        //对类型进行判断
        if (type == 1) {
            //是菜品--判断菜品是否为空
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dish::getCategoryId,id);
            int count = dishService.count(queryWrapper);
            if (count > 0) {
                //有菜品--不可以删除
                return R.error("该菜品分类下有菜品，不可以删除");
            }
        } else {
            //是套餐
            //是菜品--判断菜品是否为空
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getCategoryId,id);
            int count = setmealService.count(queryWrapper);
            if (count > 0) {
                //有菜品--不可以删除
                return R.error("该套餐分类下有套餐，不可以删除");
            }
        }
        //可以删除
        categoryService.removeById(id);
        return R.success("可以删除");
    }

    /**
     * 修改菜品
     * @param category
     * @return
     */
    @PutMapping
    public R updateById(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R list(Category category){
//        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
//        //queryWrapper.eq("type",category.getType());
//        queryWrapper.orderByAsc("sort");
//        queryWrapper.orderByDesc("update_time");
//        List<Category> list = categoryService.list(queryWrapper);
//        return R.success(list);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(category.getType() != null, "type", category.getType());
        queryWrapper.orderByDesc("update_time");
        List<Category> categoryList = categoryService.list(queryWrapper);
        return R.success(categoryList);
    }
}
